#!/usr/bin/env python

import os
import smtplib
import mimetypes
#from email.MIMEMultipart import MIMEMultipart
#from email.MIMEBase import MIMEBase
from email.MIMEText import MIMEText
#from email.MIMEAudio import MIMEAudio
#from email.MIMEImage import MIMEImage
#from email.Encoders import encode_base64
import optparse

def sendMail(recipient, subject, text):
  gmailUser = 'jtan.mailservice@gmail.com'
  gmailPassword = '{E4B7DF43-6AE1-47B2-9B4A-5A96A30FC508}'  

  msg = MIMEText(text)
  msg['From'] = gmailUser
  msg['To'] = recipient
  msg['Subject'] = subject  
  
  mailServer = smtplib.SMTP('smtp.gmail.com', 587)
  mailServer.ehlo()
  mailServer.starttls()
  mailServer.ehlo()
  mailServer.login(gmailUser, gmailPassword)
  mailServer.sendmail(gmailUser, recipient, msg.as_string())
  mailServer.close()

  print('Sent email to %s' % recipient)

if __name__ == "__main__":
  p = optparse.OptionParser()
  p.add_option("-r", "--recipient", action="store", type="string", dest="recipient")
  p.add_option("-t", "--titile", action="store", type="string", dest="title", default="")
  p.add_option("-m", "--message", action="store", type="string", dest="message", default="")
  p.add_option("-M", "--msgFile", action="store", type="string", dest="msgFile", default="")
  
  (options, args) = p.parse_args()
  
  message = options.message
  if len(options.msgFile)>0 and os.path.exists(options.msgFile):
    with open(options.msgFile, 'r') as content_file:
      message = content_file.read()
  
  sendMail(options.recipient, options.title, message)
